package com.w3coffee.web.dao;

import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.w3coffee.web.dao.model.*;

public class CoffeeDao {
	private SqlSessionFactory sessionFactory;

	public CoffeeDao() throws Exception {
		String resource = "com/w3coffee/web/dao/sqlMapper.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		sessionFactory = new SqlSessionFactoryBuilder().build(reader);
	}

	public void createUser(TUser tu) throws Exception {
		SqlSession session = sessionFactory.openSession(true);
		try {
			TUserMapper mapper = session.getMapper(TUserMapper.class);
			mapper.insert(tu);
		} finally {
			session.close();
		}
	}
	
	public int getUserId(TUser tu) throws Exception {
		SqlSession session = sessionFactory.openSession(true);
		try {
			TUserMapper mapper = session.getMapper(TUserMapper.class);
			TUserExample example = new TUserExample();
			example.createCriteria().andIdEqualTo(tu.getId()).andHostEqualTo(tu.getHost());
			List<TUser> records = mapper.selectByExample(example);
			if (records != null) {
				return records.get(0).getUid();
			} else {
				return -1;
			}
		} finally {
			session.close();
		}
	}
	
	public void updateUser(TUser tu) throws Exception {
		SqlSession session = sessionFactory.openSession(true);
		try {
			TUserMapper mapper = session.getMapper(TUserMapper.class);
			mapper.updateByPrimaryKey(tu);
		} finally {
			session.close();
		}
	}
	
	public TUser getUser(int uid) throws Exception {
		SqlSession session = sessionFactory.openSession(true);
		try {
			TUserMapper mapper = session.getMapper(TUserMapper.class);
			return mapper.selectByPrimaryKey(uid);
		} finally {
			session.close();
		}
	}
	
	public List<TBranch> getAllBranchs() throws Exception {
		SqlSession session = sessionFactory.openSession(true);
		try {
			TBranchMapper mapper = session.getMapper(TBranchMapper.class);
			TBranchExample example = new TBranchExample();
			return mapper.selectByExample(example);
		} finally {
			session.close();
		}
	}
	
	public List<TMeal> getAllMeals() throws Exception {
		SqlSession session = sessionFactory.openSession(true);
		try {
			TMealMapper mapper = session.getMapper(TMealMapper.class);
			TMealExample example = new TMealExample();
			return mapper.selectByExample(example);
		} finally {
			session.close();
		}
	}
	
	public List<TNews> getNews(int count) throws Exception {
		SqlSession session = sessionFactory.openSession(true);
		try {
			TNewsMapper mapper = session.getMapper(TNewsMapper.class);
			return mapper.selectByPage(0, count);
		} finally {
			session.close();
		}
	}
	
	public TCheckin getCheckinDetail(int cid) throws Exception {
		SqlSession session = sessionFactory.openSession(true);
		try {
			TCheckinMapper mapper = session.getMapper(TCheckinMapper.class);
			return mapper.selectByPrimaryKey(cid);
		} finally {
			session.close();
		}
	}
	
	public void createCheckinDetail(TCheckin record) throws Exception {
		SqlSession session = sessionFactory.openSession(true);
		try {
			TCheckinMapper mapper = session.getMapper(TCheckinMapper.class);
			mapper.insert(record);
		} finally {
			session.close();
		}
	}
}
